package com.zy.dao.impl;

import com.zy.dao.DeleteFromCarDao;
import com.zy.utils.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DeleteFromCarDaoImpl implements DeleteFromCarDao {

    @Override
    public boolean decreaseQuantity(int userId, int shopId) {
        String sql = "UPDATE tb_car SET quantity = quantity - 1 WHERE user_id = ? AND shop_id = ? AND quantity > 1";
        try (Connection connection = JdbcUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, userId);
            preparedStatement.setInt(2, shopId);
            int rowsAffected = preparedStatement.executeUpdate();
            System.out.println("Executing SQL: " + sql);
            System.out.println("User ID: " + userId);
            System.out.println("Shop ID: " + shopId);
            System.out.println("Rows affected: " + rowsAffected);
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
